﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace ProjectSurvivor
{
    public class AchievementSystem : AbstractSystem
    {
        public static EasyEvent<AchievementItem> OnAchievementUnlocked = new EasyEvent<AchievementItem>();
        public List<AchievementItem> Items = new List<AchievementItem>();

        public AchievementItem Add(AchievementItem item)
        {
            Items.Add(item);
            return item;
        }
        
        protected override void OnInit()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            
            ActionKit.OnUpdate.Register(() =>
            {
                if (Time.frameCount % 10 == 0)
                {
                    foreach (var achievementItem in Items.Where(item => !item.Unlocked && item.CheckCondition()))
                    {
                        achievementItem.Unlock(saveSystem);
                    }
                }
            });

            Add(new AchievementItem()
                .WithKey("3_minutes")
                .WithName("坚持3分钟")
                .WithDescription("坚持3分钟\n奖励10金币")
                .WithIconName("achievement_time_icon")
                .Condition(() => Global.CurrentSeconds.Value >= 60 * 3)
                .OnUnlocked(_ => Global.Coin.Value += 10)
                .Load(saveSystem));
            
            Add(new AchievementItem()
                .WithKey("5_minutes")
                .WithName("坚持5分钟")
                .WithDescription("坚持5分钟\n奖励100金币")
                .WithIconName("achievement_time_icon")
                .Condition(() => Global.CurrentSeconds.Value >= 60 * 5)
                .OnUnlocked(_ => Global.Coin.Value += 100)
                .Load(saveSystem));
            
            Add(new AchievementItem()
                .WithKey("10_minutes")
                .WithName("坚持10分钟")
                .WithDescription("坚持10分钟\n奖励1000金币")
                .WithIconName("achievement_time_icon")
                .Condition(() => Global.CurrentSeconds.Value >= 60 * 10)
                .OnUnlocked(_ => Global.Coin.Value += 1000)
                .Load(saveSystem));
            
            Add(new AchievementItem()
                .WithKey("lv30")
                .WithName("等级为30级")
                .WithDescription("将等级升到30级\n奖励100金币")
                .WithIconName("achievement_level_icon")
                .Condition(() => Global.Level.Value >= 30)
                .OnUnlocked(_ => Global.Coin.Value += 100)
                .Load(saveSystem));
            
            Add(new AchievementItem()
                .WithKey("lv50")
                .WithName("等级为50级")
                .WithDescription("将等级升到50级\n奖励300金币")
                .WithIconName("achievement_level_icon")
                .Condition(() => Global.Level.Value >= 50)
                .OnUnlocked(_ => Global.Coin.Value += 300)
                .Load(saveSystem));
        }
    }
}